package com.lyz.moil.core.utils;

import java.io.StringWriter;
import java.util.Map;

import org.jboss.logging.Logger;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * Created with IntelliJ IDEA. User: xi Date: 13-9-26 Time: PM11:34 To change
 * this template use File | Settings | File Templates.
 */
public class FreemarkerUtils {

	private static Logger logger = Logger.getLogger(FreemarkerUtils.class);
	
	private static Configuration cfg;
	private static Template template;
	private static StringWriter stringWriter;

	private static String TEMPLATE_PATH = "/template/";

	public static String getMsgFromTemplate(Map params, String ftlName) {

		cfg = new Configuration();
		cfg.setClassForTemplateLoading(FreemarkerUtils.class, TEMPLATE_PATH);

		stringWriter = new StringWriter();

		try {
			template = cfg.getTemplate(ftlName, "utf-8");
			template.process(params, stringWriter);
		} catch (Exception e) {
			logger.error(e);
		}
		return stringWriter.toString();
	}
	
	public static String getMsgFromTemplate(String ftlName) {
		return getMsgFromTemplate(null, ftlName);
	}
	
	public static void main(String[] args) {
		System.out.println(getMsgFromTemplate(null, "jbpm-task-msg.ftl"));
	}

}
